<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Wyp
 */

namespace App\Modules\Live\Repositories;


use App\Models\LiveMember;
use App\Modules\Live\Validators\LiveMemberValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;
use App\Exceptions\AllValidatorException;

class LiveMemberRepository extends BaseRepository
{
    public function model()
    {
        return LiveMember::class;
    }

    public function validator()
    {
        return LiveMemberValidator::class;
    }

    /**
     * 用户进入直播间
     * @param $member_id
     * @param $live_id
     * @return mixed
     */
    public function createLiveMember($member_id,$live_id)
    {
        $data = [
            'member_id' => $member_id,
            'live_id' => $live_id,
            'status' => 1,
        ];

        return $this->create($data);
    }

    /**
     * 退出
     * @param $member_id
     * @param $live_id
     * @return mixed
     * @throws AllValidatorException
     */
    public function editLiveMember($member_id,$live_id)
    {
        $liveMember = $this->getLiveMember($member_id,$live_id);
        if(!$liveMember) {
            throw new AllValidatorException(['msg' => '数据不存在']);
        }
        $data = [
            'status' => 0,
        ];
        return $this->update($data,$liveMember->id);
    }

    /**
     * 获取数据信息
     * @param $member_id
     * @param $live_id
     * @return mixed
     */
    public function getLiveMember($member_id,$live_id)
    {
        $liveMember = $this->findWhere(['member_id'=>$member_id,'live_id'=>$live_id,'status'=>1],['id','status'])->first();
        return $liveMember;
    }

    /**
     * 获取在线人数
     * @param $live_id
     * @return mixed
     */
    public function getMemberNum($live_id)
    {
        $liveMemberNum = $this->findWhere(['live_id'=>$live_id,'status'=>1])->count();
        return $liveMemberNum;
    }

    public function getMemberNumAll($live_id)
    {
        $liveMemberNum = $this->findWhere(['live_id'=>$live_id])->count();
        return $liveMemberNum;
    }
}